# coding=utf8
import json

import falcon

from models.resource.classify import get_classify
from models.resource.crawl import model_res_crawl


class ListResource:
    def on_options(self, req, resp):
        resp.status = falcon.HTTP_200  # This is the default status
        resp.set_header('Access-Control-Allow-Origin', '*')
        resp.set_header('Access-Control-Allow-Headers', 'uid')
        resp.set_header('Access-Control-Allow-Methods', 'POST,GET')

    def on_get(self, req, resp):
        result = []
        for classify_name, classify_id in get_classify().items():
            result.append({
                'classify_name': classify_name,
                'classify_id': str(classify_id)
            })

        resp.status = falcon.HTTP_200  # This is the default status
        resp.set_header('Access-Control-Allow-Origin', '*')
        resp.set_header('Access-Control-Allow-Headers', 'uid')
        resp.body = json.dumps({
            'info': {
                'code': 200
            },
            'item': result
        })
